package com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import com.android.R;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;

public final class common {
	static String TAG="AndroidReport";
	public static final String getCallDetails(Context context) {
		StringBuffer stringBuffer = new StringBuffer();
		Cursor cursor = context.getContentResolver().query(
				CallLog.Calls.CONTENT_URI, null, null, null,
				CallLog.Calls.DATE + " DESC");
		int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
		int type = cursor.getColumnIndex(CallLog.Calls.TYPE);
		int date = cursor.getColumnIndex(CallLog.Calls.DATE);
		int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);

		try {
			// Loop on cursor CallLogs
			while (cursor.moveToNext()) {
				String phNumber = cursor.getString(number);
				String callType = cursor.getString(type);
				String callDate = cursor.getString(date);
				Date callDayTime = new Date(Long.valueOf(callDate));
				String callDuration = cursor.getString(duration);
				String dir = null;
				int dircode = Integer.parseInt(callType);
				switch (dircode) {
				case CallLog.Calls.OUTGOING_TYPE:
					dir = context.getString(R.string.out);
					break;
				case CallLog.Calls.INCOMING_TYPE:
					dir = context.getString(R.string.in);
					break;
				case CallLog.Calls.MISSED_TYPE:
					dir = context.getString(R.string.miss);
					break;
				}
				stringBuffer.append(common.SearchContact(context, phNumber)
						+ "\t" + phNumber + "\t" + dir + "\t" + callDayTime
						+ "\t" + callDuration+"\n");
			}

		} catch (Exception e) {
			Log.e(TAG, e.getLocalizedMessage());
		} finally {
			cursor.close();
		}
		return stringBuffer.toString();
	}

	public static String SearchContact (Context context, String number){
		String contactName="";
		try{
		ContentResolver localContentResolver = context.getContentResolver();
        // Find ContactName with input 'phNumber'
	    Cursor contactLookupCursor = localContentResolver.query(
                Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
                Uri.encode(String.valueOf(number))), 
                new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, 
                null, 
                null, 
                null);  	    
	    
	    while(contactLookupCursor.moveToNext()){
	       contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
	    }
		}catch(Exception e){
			e.printStackTrace();
		}
	    return contactName;
	}
	
	public static ArrayList<String> GetConfig(Context context){
		Log.i(TAG, "Read file config.xml");
		String path=context.getFilesDir()+"/config.xml";
		File outFile = new File(path);
		ArrayList<String> list=new ArrayList<String>();
		Log.i(TAG, "File size:" + outFile.length());
		try {
			if (outFile.exists()) {		
				String line = "";
				BufferedReader br = new BufferedReader(new FileReader(outFile));
				while ((line = br.readLine()) != null) {
					list.add(line);
				}
				br.close();
			} else {
				Log.e(TAG, "File:" + path + " not exist");
			}

		} catch (Exception e) {
			Log.e(TAG, e.getLocalizedMessage());
		}		
		return list;
	}
	
	public static boolean WriteConfig(Context context,ArrayList<String> listConfig){
		Log.i(TAG, "Write file config.xml");
		String path=context.getFilesDir()+"/config.xml";
		File inFile = new File(path);
		try {
			inFile.createNewFile();
			FileWriter writer=new FileWriter(inFile);
			for (String item : listConfig) {
				writer.write(item+"\n");
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}	
		return true;
	}
}
